package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Demo02Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object username = req.getServletContext().getAttribute("username");
        System.out.println(username);
        //获取请求的资源路径：/demo0914_01/demo02.do
        System.out.println(req.getRequestURI());
        //获取统一资源定位符：http://localhost:8083/demo0914_01/demo02.do
        System.out.println(req.getRequestURL());
        //获取客户端的IP地址
        System.out.println(req.getRemoteHost());
        //获取请求头信息
        System.out.println(req.getHeader("Accept-Language"));
        System.out.println(req.getHeader("Cookie"));
        System.out.println(req.getHeader("Accept"));
        //获取请求参数
        //获取单个值
        System.out.println(req.getParameter("username"));
        //获取同名的多个值  常用于多选框
        String[] hobbies = req.getParameterValues("hobby");
        for (int i = 0; i < hobbies.length; i++) {
            System.out.println(hobbies[i]);
        }
        //获取本次请求方式
        String method = req.getMethod();
        System.out.println(method);
        //request作为域对象
//        req.getAttribute()
//        req.setAttribute();
        //获取请求转发对象
//        req.getRequestDispatcher()
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
